#include "StdAfx.h"
#include "P2PSessionManager.h"
#include "P2P.h"

INT32 CSvrPacketParser::ParsePacket( const char* pData, const INT32 iLen )
{
    if (iLen < sizeof(INT32))
    {
        return 0;
    }

    INT32 iPkglen = *(INT32*)pData;

    if (iPkglen > iLen)
    {
        return 0;
    }

    return iPkglen;
}

CP2PSessionManager::CP2PSessionManager( CP2P* p2p )
{
    m_p2p = p2p;
}

CP2PSessionManager::~CP2PSessionManager(void)
{
}

void CP2PSessionManager::OnEstablish( HNETHANDLE hHandle, HSESSION hSession )
{
    if (hHandle == hSession)
    {
        ISession* session = (ISession*)m_p2p->get_net_manager()->GetSessionUserData(hSession);
        session->OnEstablish(hHandle, hSession);
    }
    else
    {
        CP2PSession* p2p_session = m_p2p_session_pool.AllocObj();
        m_p2p->get_net_manager()->SetSessionUserData(hSession, p2p_session);
        p2p_session->Reset();
        p2p_session->set_p2p(m_p2p);
        p2p_session->OnEstablish(hHandle, hSession);
    }
}

void CP2PSessionManager::OnTerminate( HSESSION hSession )
{
    ISession* session = (ISession*)m_p2p->get_net_manager()->GetSessionUserData(hSession);
    if (!session)
        return;
    session->OnTerminate(hSession);
}

void CP2PSessionManager::OnError( HNETHANDLE hHandle, HSESSION hSession, INT32 iModuleError, INT32 iSystemError )
{
    ISession* session = (ISession*)m_p2p->get_net_manager()->GetSessionUserData(hSession);
    if (!session)
        return;
    session->OnError(hHandle, hSession, iModuleError, iSystemError);
}

void CP2PSessionManager::OnRecv( HSESSION hSession, const char* pData, const INT32 iLen )
{
    ISession* session = (ISession*)m_p2p->get_net_manager()->GetSessionUserData(hSession);
    if (!session)
        return;
    session->OnRecv(hSession, pData, iLen);
}

bool CP2PSessionManager::AddP2PSession( CP2PSession& p2p_session )
{
    if (m_p2p_session_map.find(p2p_session.get_p2p_id()) != m_p2p_session_map.end())
    {
        return false;
    }

    m_p2p_session_map[p2p_session.get_p2p_id()] = &p2p_session;

    return true;
}

CP2PSession* CP2PSessionManager::FindP2PSession( UINT32 p2p_id )
{
    TP2PSsessionIter it = m_p2p_session_map.find(p2p_id);
    if (it == m_p2p_session_map.end())
    {
        return NULL;
    }

    return it->second;
}

void CP2PSessionManager::DelP2PSession( UINT32 p2p_id )
{
    m_p2p_session_map.erase(p2p_id);
}

bool CP2PSessionManager::AddPeerInfo( PeerInfo& peer_info )
{
    if (m_peer_info_map.find(peer_info.p2p_id) != m_peer_info_map.end())
    {
        return false;
    }

    m_peer_info_map[peer_info.p2p_id] = &peer_info;

    return true;
}

PeerInfo* CP2PSessionManager::FindPeerInfo( UINT32 p2p_id )
{
    TPeerInfoIter it = m_peer_info_map.find(p2p_id);
    if (it == m_peer_info_map.end())
    {
        return NULL;
    }

    return it->second;
}

void CP2PSessionManager::DelPeerInfo( UINT32 p2p_id )
{
    m_peer_info_map.erase(p2p_id);
}
